package person.bing.fbcloud.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @author: mengfanbing
 * @date: 2020/7/31 10:02
 */
public class Q18 {
    public static void main(String[] args) {
        int[] nums = {1,-2,-5,-4,-3,3,3,5};
        int target = -11;
        List<List<Integer>> lists = fourSum(nums, target);
        System.out.println(lists);
    }

    //双指针
    public static List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> ans = new ArrayList<>();
        int length = nums.length;
        Arrays.sort(nums);
        for (int i=0;i<length-3;i++){
            if (i>0 && nums[i] == nums[i-1]){
                continue;
            }
            if (nums[i] > 0 && nums[i] > target){
                break;
            }
            int min = nums[i] + nums[i+1] + nums[i+2] + nums[i+3];
            if (min > target){
                break;
            }
            int max = nums[length-1] + nums[length-2] + nums[length-3] + nums[i];
            if (max < target){
                continue;
            }
            for (int j=i+1;j<length-2;j++){
                if (j>i+1 && nums[j] == nums[j-1]){
                    continue;
                }
                if (nums[j] > 0 && nums[j] > target){
                    return ans;
                }
                int k = j+1;
                int l = length-1;
                while (k < l){
                    if (k>j+1 && nums[k]==nums[k-1]){
                        k++;
                        continue;
                    }
                    if (l<length-1 && nums[l]==nums[l+1]){
                        l--;
                        continue;
                    }
                    int sum = nums[i] + nums[j] + nums[k] + nums[l];
                    if (sum == target){
                        List<Integer> list = new ArrayList<>();
                        list.add(nums[i]);
                        list.add(nums[j]);
                        list.add(nums[k]);
                        list.add(nums[l]);
                        ans.add(list);
                        k++;
                        l--;
                    }else if (sum < target){
                        k++;
                    }else{
                        l--;
                    }
                }
            }
        }
        return ans;
    }


    //四层循环
    public static List<List<Integer>> fourSum2(int[] nums, int target) {
        List<List<Integer>> ans = new ArrayList<>();
        int length = nums.length;
        Arrays.sort(nums);
        for (int i=0;i<length-3;i++){
            if (i>0 && nums[i] == nums[i-1]){
                continue;
            }
            for (int j=i+1;j<length-2;j++){
                if (j>i+1 && nums[j] == nums[j-1]){
                    continue;
                }
                for (int z=j+1;z<length-1;z++){
                    if (z>j+1 && nums[z] == nums[z-1]){
                        continue;
                    }
                    for (int x=z+1;x<length;x++){
                        if (x>z+1 && nums[x] == nums[x-1]){
                            continue;
                        }
                        if ((nums[i] + nums[j] + nums[z] + nums[x]) == target){
                            List<Integer> list = new ArrayList<>();
                            list.add(nums[i]);
                            list.add(nums[j]);
                            list.add(nums[z]);
                            list.add(nums[x]);
                            ans.add(list);
                        }
                    }
                }
            }
        }
        return ans;
    }
}
